/**************************************************************************//**
 * @file           main.c
 * @version        V1.0
 * $Revision:      1 $
 * $Date:          2017/01/04 $
 * @brief          Main function
 * @Author         Jeffrey Kang
 * @ContactInfo    chkang@esmt.com.tw
 *
 * @note
 *
 * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
 * All rights reserved.
 *
 ******************************************************************************/


#include <REG52.h>
#include "system_FE81001.h"


static volatile unsigned char clb_data[3] = {0x11, 0x22, 0x33};
static volatile unsigned char cnt = 0;


/**
 * @brief       delay function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void delay (void)
{
	unsigned int i;
	
	for( i = 0 ; i < 200 ; i++ ){}
}


/**
 * @brief       I2CSISR function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
const void RTCISR (void) interrupt ISR_RTC
{
	unsigned short int_stat = RTC->rtc_stat;
	
	/* alarm interrupt */
	if ((int_stat & 0x04) == 0x04){
		RTC->rtc_stat = 0x04;
		
		PD3 = ~PD3; // Toggle LED_G2
		
	}
	/* day interrupt */
	if ((int_stat & 0x08) == 0x08){
		RTC->rtc_stat = 0x08;
		
	}
	
	/* hour interrupt */
	if ((int_stat & 0x10) == 0x10){
		RTC->rtc_stat = 0x10;
		
	}
	
	/* min interrupt */
	if ((int_stat & 0x20) == 0x20){
		RTC->rtc_stat = 0x20;
		
		PD2 = ~PD2; // Toggle LED_R1
	}	
	
	/* sec interrupt */
	if ((int_stat & 0x40) == 0x40){
		RTC->rtc_stat = 0x40;
		
		PD1 = ~PD1; // Toggle LED_G1
	}
	
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
}


/**
 * @brief       RTCConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void RTCConfig()
{	
		/* set current time */
	RTC->cnt_sec  = 0x1A;
	RTC->cnt_min  = 0x14;
	RTC->cnt_hour = 0x10;
	RTC->cnt_day  = 0x00;
	
	/* set alarm time */
	RTC->alm_sec  = 0x1C;
	RTC->alm_min  = 0x14;
	RTC->alm_hour = 0x10;
	
	/* enable rtc interrupt */
	RTC->rtc_ctrl = 0x75;               // 0, sec = 1, min = 1, hour = 1, day = 0, alarm_int = 1, alarm_wakeup = 0, rtc_en = 1
	
		/* enable global interrupt */
	INTC->int_gmask = 0x01;
	
	/* wait write_done */
	while(!RTC->write_done){}
}


/**
 * @brief       IOConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void IOConfig()
{
	/* enable GPIO clock */
	PORT_PAPBPCPD_Clock_Enable(); // enable GPIOA GPIOB GPIOC GPIOD clock
	
	/* Set PC7 input as button */
	PORT_SetPCInput(BIT7);

	/* Set PD1. ,PD2.,PD3 output */
	PORT_SetPDPushPull(BIT1 | BIT2 | BIT3 );	
}


/**
 * @brief       CLKConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void CLKConfig()
{
	/* rtc clock source select */
	Set_RTC_ClockSource(Low_speed_oscillator);
	
	/* enable RTC clock */
	En_RTC_clk();
	
	/* wait LSRC stable */
	while((CLKC->clkstb & 0x04) != 0x04){}
	
	/* wait clock selection ready */
	delay();
}


/**
 * @brief       main function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
int main (void)
{
	CLKConfig();
	IOConfig();
	
	RTCConfig();
	
	PD1=1; //LED_G1 Off
	PD2=1; //LED_R Off
	PD3=1; //LED_G2 Off
	
	
	
	while(1)
	{
//		if( Get_PCx_input(7) == 0)   // PC7  = 0 , button down
//			{
//					PD2 = 0 ; //LED_R On
//			}
//			else
//			{
//					PD2 = 1 ; //LED_R Off
//			}
	}
}

